package com.fairytask.ui
{
	import com.fairytask.engine.components.BaseItem;
	import com.fairytask.engine.components.ToolTips;
	import com.fairytask.engine.components.button.Button;
	import com.fairytask.engine.components.clip.Clip;
	import com.fairytask.engine.components.group.ViewGroup;
	import com.fairytask.engine.components.text.Label;
	import com.fairytask.engine.define.Filter;
	import com.fairytask.engine.define.ResConfig;
	import com.fairytask.engine.managers.ResManager;
	import com.fairytask.engine.resource.StaticIni;
	import com.jzonl.managers.TipsManage;
	import com.jzonl.net.event.CustomToClientEvent;
	import com.jzonl.net.messages.CustomToClientEvent;
	import com.jzonl.net.messages.CustomToServerFlag;
	import com.jzonl.net.mgr.NetMgr;
	import com.fairytask.ui.base.FrameUI;
	import com.fairytask.ui.item.SceneItem;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	/**
	 *	野外场景UI
	 * @author xujh
	 * 
	 */
	public class WildBattleUI extends FrameUI
	{
		private static var _self	:WildBattleUI;
		
		//==============面板资源====================
		public var nameLab		:Label;
		public var sceneVG		:ViewGroup;
		public var comptBoxBtn	:MovieClip;		//宝箱
		public var starBoxBtn	:MovieClip;		//宝箱
		public var itemLab		:Clip;
		public var itemVG		:ViewGroup;
		public var enterBtn	:Button;		//进入
		public var offlineBtn	:Button;		//挂机
		public var comptBar	:MovieClip;
		public var starBG		:MovieClip;
		public var starSp		:Sprite;
		
		//================================
		private var _cureentScene	:int;	//当前选中地图
		private var _clickID	:int		=	-1;	//选中的场景
		private var _sceneID	:int;		//场景id
		private var _infoUI	:InfoUI = InfoUI.getInstance();
		//可用挂机
		private var _canOffLine	:Boolean = false;
		//场景已经通关
		private var _isPass	:Boolean = false;
		//
		
		public static function getInstance():WildBattleUI
		{
			if(null	==	_self)
			{
				_self	=	new WildBattleUI();				
			}
			return _self;
		}
		
		public function WildBattleUI(popUI:Boolean=true)
		{
			super(popUI);
		}
		
		/**
		 * 重写显示 
		 * 
		 */
		override public function initGUI():void
		{
			starSp.x = 25;
			starSp.y = 350;
//			starSp.scrollRect	=	new Rectangle(0,20,25,-320);
			//
			comptBar.BarBg.width = 430;
			
			//宝箱初始化
			comptBoxBtn.width = starBoxBtn.width = 30;
			comptBoxBtn.height = starBoxBtn.height = 27;
			initEvent();
			initVG();
		}
		
		/**
		 * 初始化事件 
		 * 
		 */
		private function initEvent():void
		{
			//点击选中
			sceneVG.addItemsEvent(MouseEvent.CLICK,onSceneClick);
			itemVG.addItemsEvent(MouseEvent.MOUSE_OVER,onItemOver);
			itemVG.addItemsEvent(MouseEvent.MOUSE_OUT,onItemOut);
			//通关宝箱
			comptBoxBtn.addEventListener(MouseEvent.CLICK,onPassBox);
			//星级宝箱
			starBoxBtn.addEventListener(MouseEvent.CLICK,onStarBox);
			//进入场景
			enterBtn.addEventListener(MouseEvent.CLICK,onEnter);
			//场景挂机
			offlineBtn.addEventListener(MouseEvent.CLICK,offLine);
			//领取宝箱
			model.addEventListener(CustomToClientEvent.SERVER_CUSTMMSG_SCENE_PRIZE_GET,getPrize);
			//掉落物品
//			model.addEventListener(CustomToClientEvent.SERVER_CUSTMMSG_AREA_DROP,dropItem);
		}
		
		/**
		 * 初始化容器 
		 * 
		 */
		private function initVG():void
		{
			for(var i:int=0;i<9;i++)
			{
				var tmpItem		:SceneItem		=	new SceneItem();
				var baseItem	:BaseItem	=	sceneVG.getItemByIndex(i);
				baseItem.addItem(tmpItem);
			}
		}
		
		/**
		 * //打开野外地图窗口
		 //string		fieldName	野外场景名称
		 //int			areaCount	区域场景块数
		 //int			areaObj		区域场景ID
		 //int			areaPhoto	区域场景地图
		 //int			areaStar	区域场景星数
		 //int			isPass		是否已通关
		 //int			isLock		是否锁定
		 //..						更多区域
		 //int			fieldStar	场景总星数
		 //int			targetStar	目标星数
		 //int			fieldPrizeStatus	场景宝箱状态 0 未开启 1 开启未领奖 2 已领奖
		 //int			passCount	通关数量
		 //int			targetPass	目标通关数量
		 //int			passPrizeStatus		通关宝箱状态  0 未开启 1 开启未领奖 2 已领奖
		 //int			dropCount	掉落物品列表
		 //int			dropid		掉落ID
		 //...						更多掉落
		 
		 SERVER_CUSTMMSG_FIELDSCENE_WINDOW	=	2270,
		 * @param evt
		 * 
		 */
		public function showScene(evt:CustomToClientEvent):void
		{
			show();
			var pList		:Array	=	evt.pList;
			//野外场景名称
			_sceneID =	pList.shift();
			nameLab.htmlStrByBorder = StaticIni.getSwfLangStr(_sceneID.toString());
			nameLab.x = (520 - nameLab.width)/2;
			var areaCount	:int	=	pList.shift();	//区域场景块数
			//清除容器内容
//			clearVg(sceneVG);
			for(var i:int=0;i<areaCount;i++)
			{
				var areaObj		:int	=	pList.shift();	//区域场景ID
				var areaPhoto	:int	=	pList.shift();	//区域场景地图
				var areaStar	:int	=	pList.shift();	//区域场景星数
				var isPass		:Boolean	=	Boolean(pList.shift());	//是否已通关
				var isLock		:Boolean	=	Boolean(pList.shift());	//是否锁定
				var baseItem	:BaseItem	=	sceneVG.getItemByIndex(i);
				var tmpItem		:SceneItem		=	baseItem.getItem() as SceneItem;
				tmpItem.fieldID 	= _sceneID.toString();
				tmpItem.configID	= areaPhoto;
				tmpItem.stars		= areaStar;
				tmpItem.locked		= isLock;
				
			}
			
			//场景星数
			var fieldStar	:int	=	pList.shift();	//场景总星数
			var targetStar	:int	=	pList.shift();	//目标星数
			setStars(fieldStar,targetStar);
			//场景评分宝箱状态 0 未开启 1 开启未领奖 2 已领奖
			setPrizeStatus(pList.shift(),starBoxBtn);
			
			//通关进度
			var passCount	:int	=	pList.shift();	//通关数量
			var targetPass	:int	=	pList.shift();	//目标通关数量
			comptBar.Bar.width		=	(passCount/targetPass)*comptBar.BarBg.width;
			//通关宝箱状态
			setPrizeStatus(pList.shift(),comptBoxBtn);
		}
		
		/**
		 * 设置星星数量 
		 * @param val
		 * 
		 */
		private function setStars(val:int,total:int):void
		{
			while(starSp.numChildren>0)
			{
				starSp.removeChildAt(0);
			}
			if(val > total)
			{
				val = total;
			}
			for(var i:int=0;i<val;i++)
			{
				var star	:MovieClip = ResManager.newResClass("CustSkin_020") as MovieClip;
//				star.scaleX = star.scaleY = 20/22;
				star.width = star.height = 20;
				star.y = -(starBG.height-5)/total*i;
				starSp.addChild(star);
			}
			
			//starSp.height	=	starSp.height>170?170:starSp.height;
		}
		
		/**
		 * 掉落物品 
		 * @param evt
		 * 
		 */
		private function dropItem(id:int):void
		{
			//清除容器
			clearVg(itemVG);
			var list		:Array = StaticIni.getIniList("Drop_"+id);
			var configID	:String;
			var pos			:int = 0;
			//添加物品
			while(list.length > 0)
			{
				configID = list.shift();
				var clip	:Clip = new Clip();
				clip.source = ResConfig.iconPath + StaticIni.getIniVar(configID,"Icon") + ResConfig.iconImg;
				clip.x	=	2;
				clip.y	=	2;
				itemVG.getItemByIndex(pos).addItem(clip);
				itemVG.getItemByIndex(pos).name = configID;
				pos++;
			}
		}
		
		/**
		 * 宝箱状态
		 * @param val 0 未开启 1 开启未领奖 2 已领奖
		 * @param prizeMc
		 * 
		 */
		private function setPrizeStatus(val:int,prizeMc:MovieClip):void
		{
			switch(val)
			{
				case 0:
					prizeMc.gotoAndStop("normal");
					prizeMc.buttonMode	= false;
					break;
				case 1:
					prizeMc.gotoAndStop("flush");
					prizeMc.buttonMode	= true;
					break;
				case 2:
					prizeMc.gotoAndStop("unallow");
					prizeMc.buttonMode	= false;
					break;
			}
			
		}
		
		/**
		 * 场景点击 
		 * @param evt
		 * @param index
		 * 
		 */
		private function onSceneClick(evt:MouseEvent,index:int):void
		{
			if((evt.currentTarget.getItem() as SceneItem).configID==0)
			{
				return;
			}
			//未解锁不可选
			if((evt.currentTarget.getItem() as SceneItem).locked)
			{
				_infoUI.txtWithBtn(StaticIni.getSwfLangStr("1000000441"),InfoUI.SINGLEBTN,[]);
				return;
			}
			var tmpItem	:SceneItem = evt.currentTarget.getItem() as SceneItem;
			_cureentScene = tmpItem.configID;
			_isPass = tmpItem.isPass;
			if(_canOffLine)
			{
				offlineBtn.enabled = _isPass;
			}
			//清除所有滤镜发光效果
			frameRemoveTween();
			//添加滤镜
			evt.currentTarget.filters	=	[Filter.YELLOW_INNER];
//			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_CLICK_AREA,_cureentScene);
			dropItem(_cureentScene);
		}
		
		/**
		 * 显示物品tips 
		 * @param evt
		 * @param index
		 * 
		 */
		private function onItemOver(evt:MouseEvent,index:int):void
		{
			if(null != evt.currentTarget.getItem())
			{
				TipsManage.ShowItemTips(uint(evt.currentTarget.name));
			}
		}
		
		private function onItemOut(evt:MouseEvent,idx:int):void
		{
			ToolTips.hide();
		}
		
		/**
		 * 进入场景
		 * //进入区域场景
		 //obj	areaobj				区域场景ID
		 CLIENT_CUSTOMMSG_ENTER_AREA					= 2483,
		 * @param evt
		 * 
		 */
		private function onEnter(evt:MouseEvent):void
		{
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_ENTER_AREA,_cureentScene);		
		}
		
		/**
		 * 打开挂机界面 
		 * //挂机
		 //obj	areaobj				区域场景ID
		 CLIENT_CUSTOMMSG_AFK					= 2484,
		 * @param evt
		 * 
		 */
		private function offLine(evt:MouseEvent):void
		{
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_AFK,_cureentScene);
		}
		
		/**
		 * 领取通关宝箱 
		 * @param evt
		 * 
		 */
		private function onPassBox(evt:MouseEvent):void
		{
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_GET_STAR_CHEST,2,_sceneID);
		}
		
		/**
		 * 领取星级宝箱 
		 * @param evt
		 * 
		 */
		private function onStarBox(evt:MouseEvent):void
		{
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_GET_STAR_CHEST,1,_sceneID);
		}
		
		/**
		 * 领取宝箱
		 * @param evt
		 * 
		 */
		private function getPrize(evt:CustomToClientEvent):void
		{
			var tpye	:int = evt.pList.shift();
			if(tpye == 1)
			{
				setPrizeStatus(2,starBoxBtn);
			}
			else
			{
				setPrizeStatus(2,comptBoxBtn);
			}
		}
		
		/**
		 * 清除选择物品项效果 
		 * 
		 */		
		private function frameRemoveTween():void
		{
			sceneVG.getItemForEach(function(tmpItem:BaseItem):void
			{
				tmpItem.filters = null;
			});	
		}
		
		/**
		 * 清空容器
		 * @param val
		 * 
		 */
		private function clearVg(val:ViewGroup):void
		{
			val.getItemForEach(function (baseItem:BaseItem):void
			{
				baseItem.data = null;
				baseItem.clear();
			});
		}
		
		/**
		 * 是否可以挂机 
		 * @param val
		 * 
		 */
		public function set canOffLine(val:Boolean):void
		{
			_canOffLine = val;
			//选中的场景已经通关
			if(_isPass)
			{
				offlineBtn.enabled = true;
			}
		}
		
		/**
		 * 重写显示 
		 * @param space
		 * @param onshow
		 * @param argc
		 * 
		 */
		override public function show(space:String=TOPLAYER, onshow:Function=null):void
		{
			//先显示世界地图
			if(!WorldMapUI.getInstance().isShow)
			{
				WorldMapUI.getInstance().show();
			}
			super.show(space,onshow);
			//清除选中
			_cureentScene = 0;
			//清除所有滤镜发光效果
			frameRemoveTween();
			offlineBtn.enabled = false;
			_isPass = false;
			//清除物品容器
			clearVg(itemVG);
		}
	}
}